package com.eam.service;

import com.yy.base.common.dao.Dao;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentService {
	@Autowired
	private Dao dao;
	
	//加载保养计划编码(组织机构是在车间下的)
	public List<Map<String,Object>> getEquipmentPlanMaintainLists(String orgId) {
		return dao.query("select *  from Eam_Maintain_Plan where DISTINGUISH='MAINTAIN' and status IN ('CREATE','SERVICE') and plan_end_time > sysdate and ad_org_id = ?", orgId);
	}
	
	//加载保养计划编码（组织机构不在车间下的）
	public List<Map<String,Object>> getEquipmentPlanMaintainList() {
		return dao.query("select *  from Eam_Maintain_Plan where DISTINGUISH='MAINTAIN' and status IN ('CREATE','SERVICE') and plan_end_time > sysdate ");
	}
	
	//加载维修计划编码（组织机构是在车间下的）
	public List<Map<String,Object>> getEquipmentPlanRepairLists(String orgId) {
		return dao.query("select *  from Eam_Maintain_Plan where DISTINGUISH='REPAIR' and status IN ('CREATE','SERVICE') and ad_org_id = ?", orgId);
	}
	
	//加载维修计划编码（组织机构不在车间下的）
	public List<Map<String,Object>> getEquipmentPlanRepairList() {
		return dao.query("select *  from Eam_Maintain_Plan where DISTINGUISH='REPAIR' and status IN ('CREATE','SERVICE')");
	}
	
	//加载设备磨具编码
	public List<Map<String,Object>> getEquipmentList(String code) {
		return dao.query("select wb.code as shebeicode from Eam_Maintain_Plan sb  " +
							" left join Eam_Maintain_Plan_Facs zj on zj.Eam_Maintain_Plan_id = sb.Eam_Maintain_Plan_id " +
							" left join Eam_Facilities wb on wb.Eam_Facilities_id = zj.Eam_Facilities_id " +
							" where sb.code = ? " +
							" and zj.status  IN ('APPROVED','SERVICE','CREATE') "
							+ " and sb.plan_end_time > sysdate", code);
	}

	
	//加载设备信息及保养项目
	public List<Map<String, Object>> getinformationList(String plancode , String code ){
		return dao.query("  select fa.name , fa.code ,fa.status , fc.name as bzname ,yh.name as yhname ,byxq.Maintain_Name , gx.name as GXNAME " +
				"from Eam_Maintain_Plan jh   " +
				"left join Eam_Maintain_Plan_Facs zj on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id " +
				" left join  Eam_Facilities  fa on zj.Eam_Facilities_id = fa.Eam_Facilities_id " +
				" left join ad_org fc on fa.ad_org_id = fc.ad_org_id " +
				" left join ad_user yh on yh.ad_user_id = zj.ad_user_id  " +
				" left join Eam_Maintain_Plan_Items byxm on byxm.Eam_Maintain_Plan_Facs_id = zj.Eam_Maintain_Plan_Facs_id  " +
				" left join Eam_Maintain byxq on byxm.Eam_Maintain_id = byxq.Eam_Maintain_id  "+
				" left join emes_process gx on zj.emes_process_id = gx.emes_process_id " +
				" where byxm.Eam_Repair_Item_id is null " +
				" and jh.code = ?    " +
				" and fa.code = ? " +
				" and byxm.status = 'CREATE' "+ 
				" and jh.plan_end_time > sysdate", plancode , code);
	}
	
	//加载设备信息
	public List<Map<String, Object>> getmaintenanceList(String code ){
		return dao.query(" select fa.name , fa.code ,fa.status , fc.name as bzname ,yh.name as yhname,gx.name as GXNAME , bxr.name as bxname , bxr.ad_user_id as bxuserId , jh.start_date as bxtime , jh.remarks ,"
				+ " (case when jh.isactive = 'Y' then '是' when jh.isactive = 'N' then '否' else '' end ) as TYPES "
				+ " from Eam_Maintain_Plan jh "
				+ " left join Eam_Maintain_Plan_Facs zj on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id "
				+ " left join  Eam_Facilities  fa on zj.Eam_Facilities_id = fa.Eam_Facilities_id "
				+ " left join ad_org fc on fa.ad_org_id = fc.ad_org_id "
				+ " left join ad_user yh on yh.ad_user_id = zj.ad_user_id  "
				+ " left join emes_process gx on zj.emes_process_id = gx.emes_process_id "
				+ " left join ad_user bxr on bxr.ad_user_id = jh.ad_user_id"
				+ " where jh.code = ? ", code);
	}
	
	
	
	//查询维修项目的ID
	public List<Map<String,Object>> getEquipmentput(String name,String code , String plancode){
		return dao.query("select sbby.Eam_Maintain_Plan_Items_id as sbid  " +
				"from Eam_Maintain_Plan_Facs zj  " +
				"left join Eam_Facilities sb on sb.Eam_Facilities_id = zj.Eam_Facilities_id   " +
				"left join Eam_Maintain_Plan jh on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id   " +
				"left join Eam_Maintain_Plan_Items sbby on sbby.Eam_Maintain_Plan_Facs_id = zj.Eam_Maintain_Plan_Facs_id  " +
				"left join Eam_Repair_Item wx on wx.Eam_Repair_Item_id = sbby.Eam_Repair_Item_id  " +
				"where  sbby.status = 'CREATE' " +
				"and sbby.Eam_Maintain_id is null " +
				"and wx.name = ? " +
				"and sb.code =  ? " +
				"and jh.code = ? ",name, code , plancode);
	}
	
	//查询维修设备层的ID
		public List<Map<String,Object>> getEquipmentputRepoir(String code , String plancode){
		
			
			return dao.query("select  zj.Eam_Maintain_Plan_Facs_id AS ITEMID, sb.Eam_Facilities_id as SBID, sb.name as SBNAME , jh.Eam_Maintain_plan_id AS PLANID"
							+ " from Eam_Maintain_Plan_Facs zj  "
							+ " left join Eam_Facilities sb on sb.Eam_Facilities_id = zj.Eam_Facilities_id   "
							+ " left join Eam_Maintain_Plan jh on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id   "
							+ " where sb.code =  ? "
							+ " and jh.code = ? ", code , plancode);
		}
	
	//查询保养项目的ID
	public List<Map<String,Object>> getEquipmentputs(String name,String code , String plancode){
		return dao.query("select sbby.Eam_Maintain_Plan_Items_id as sbid , jh.Eam_Maintain_plan_id as planid  " +
				" from Eam_Maintain_Plan_Facs zj  " +
				" left join Eam_Facilities sb on sb.Eam_Facilities_id = zj.Eam_Facilities_id   " +
				" left join Eam_Maintain_Plan jh on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id   " +
				" left join Eam_Maintain_Plan_Items sbby on sbby.Eam_Maintain_Plan_Facs_id = zj.Eam_Maintain_Plan_Facs_id  " +
				" left join Eam_Maintain wx on wx.Eam_Maintain_id = sbby.Eam_Maintain_id  " +
				" where  sbby.status = 'CREATE' " +
				" and sbby.Eam_Repair_Item_id is null " +
				" and wx.MAINTAIN_NAME = ? " +
				" and sb.code =  ?" +
				" and jh.code = ? " + 
				" and jh.plan_end_time > sysdate", name,code , plancode);
	}
	
	//查询保养项目的责任人ID
	public Map<String, Object> getEquipmentuserId(String username){
		return dao.queryUnique("select ad_user_id as userid from ad_user where name= ?", username);
	}

	
	//修改保养项目的状态
	@SuppressWarnings("unchecked")
	public boolean getEquipmentputUpdate(String id) {
		boolean result = false;
		
		
		String sql =  "UPDATE Eam_Maintain_Plan_Items SET status = 'OK' WHERE Eam_Maintain_Plan_Items_id = ?";
		
		result = dao.executeSql(sql, id) > 0;
		
		
		return result;
	}
	
	//EmesMaintainPlanDetails加入保养说明
	@SuppressWarnings("unchecked")
	public boolean SaveEquipmentPlanDetails(String id,String RemarksNames) {
		boolean result = false;
		
		
		String sql =  "UPDATE Eam_Maintain_Plan_Items SET DESCRIPTION = ? WHERE Eam_Maintain_Plan_Items_id = ?";
		
		result = dao.executeSql(sql, RemarksNames,id) > 0;
		
		
		return result;
	}
	
	//EmesMaintainPlanDetails加入维修保养责任人
		@SuppressWarnings("unchecked")
		public boolean SaveEquipmentUserDetails(String id,String userIdtime) {
			boolean result = false;
			
			
			String sql =  "UPDATE Eam_Maintain_Plan_Items SET ad_user_id = ? WHERE Eam_Maintain_Plan_Items_id = ?";
			
			result = dao.executeSql(sql, userIdtime,id) > 0;
			
			
			return result;
		}
		
		//EmesMaintainPlanItem加入维修信息并修改状态
		@SuppressWarnings("unchecked")
		public boolean SaveEquipmentUserItem(String id,String gztypes,String method,String Measures,String orgname,String Remarks,String OK,String userIdtime,double repairtimeVal) {
			boolean result = false;
					
					
			String sql =  "update  Eam_Maintain_Plan_Facs set GZTYPES= ? , METHOD = ? ,MEASURES = ?  ,DESCRIPTION = ? ,STATUS = ? ,AD_USER_ID = ?,GROUPORG = ?,REPAIRTIME=? where Eam_Maintain_Plan_Facs_id = ? ";
					
			result = dao.executeSql(sql, gztypes,method,Measures,Remarks,OK,userIdtime,orgname,repairtimeVal,id) > 0;
					
					
			return result;
		}
		
		//EmesMaintainPlan修改状态
				@SuppressWarnings("unchecked")
				public boolean SaveEquipmentPlan(String id,String OK,Date time) {
					boolean result = false;
					
					String types= "N";
							
					String sql =  "update  Eam_Maintain_plan set STATUS = ? , END_DATE = ? ,ISACTIVE = ? where Eam_Maintain_plan_id = ? ";
							
					result = dao.executeSql(sql, OK,time,types,id) > 0;
							
							
					return result;
				}
	
	//查询所有要存入记录表的信息
	public List<Map<String, Object>> getAllEquipemt(String id ){
		return dao.query(" select  sb.Eam_Facilities_id as facilitiesid, jh.Eam_Maintain_plan_id as emesmaintainplanid,bm.Eam_Maintain_id as emesmaintainid ,aaa.Eam_Repair_Item_id as EmesREPAIRID " +
				"from Eam_Maintain_Plan_Items  jo " +
				"left join  Eam_Maintain_Plan_Facs zj on zj.Eam_Maintain_Plan_Facs_id = jo.Eam_Maintain_Plan_Facs_id " +
				"left join  Eam_Facilities sb on sb.Eam_Facilities_id = zj.Eam_Facilities_id " +
				"left join Eam_Maintain_Plan jh on jh.Eam_Maintain_plan_id = zj.Eam_Maintain_plan_id " +
				"left join Eam_Maintain bm on bm.Eam_Maintain_id = jo.Eam_Maintain_id " +
				"left join Eam_Repair_Item aaa on aaa.Eam_Repair_Item_id = jo.Eam_Repair_Item_id " +
				"where jo.Eam_Maintain_Plan_Items_id = ?", id);
	}

	//待了维修项目的存储	
//	//保存设备维修信息
//	@SuppressWarnings("unchecked")
//	public boolean SavaEquipmentputInsert(Date time,String facilitiesid,String emesmaintainplanid,String aduerId,String emesrepairid,String Type,String status,String RemarksNames,String adorgId , String adclientId,String createBy,String updatedBy) {
//		boolean result = false;
//		String uuid = GlobalUtils.getUUID();
//		String lastRest = "Y";
//		String sql =  "INSERT INTO Eam_Facilities_MT_Logs (ISACTIVE,Eam_Facilities_MT_Logs_ID,Eam_Facilities_ID, Eam_Maintain_PLAN_ID,AD_USER_ID,Eam_Repair_Item_ID,TYPE,STATUS,DESCRIPTION,AD_ORG_ID,AD_CLIENT_ID,CREATED,CREATEDBY,UPDATED,UPDATEDBY) " +
//				"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//		
//		result = dao.executeSql(sql, lastRest,uuid,facilitiesid,emesmaintainplanid,aduerId,emesrepairid,Type,status,RemarksNames,adorgId,adclientId,time,createBy,time,updatedBy) > 0;
//		
//		return result;
//	}
	
	
	//保存设备维修信息（没带维修项目）
		@SuppressWarnings("unchecked")
		public boolean SavaEquipmentputInsert(Date time,String facilitiesid,String planId,String userIdtime,String Type,String status,String Remarks,String adorgId , String adclientId,String createBy,String updatedBy) {
			boolean result = false;
			String uuid = StringUtil.getUUID();
			String lastRest = "Y";
			String sql =  "INSERT INTO Eam_Facilities_MT_Logs (ISACTIVE,Eam_Facilities_MT_Logs_ID,Eam_Facilities_ID, Eam_Maintain_PLAN_ID,AD_USER_ID,TYPE,STATUS,DESCRIPTION,AD_ORG_ID,AD_CLIENT_ID,CREATED,CREATEDBY,UPDATED,UPDATEDBY) " +
					"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			
			result = dao.executeSql(sql, lastRest,uuid,facilitiesid,planId,userIdtime,Type,status,Remarks,adorgId,adclientId,time,createBy,time,updatedBy) > 0;
			
			return result;
		}
	
	//保存设备保养信息
	@SuppressWarnings("unchecked")
	public boolean InsertEquipmentputInserts(Date time,String facilitiesid,String emesmaintainplanid,String aduerId,String emesmaintainid,String Types,String status,String RemarksNames,String adorgId , String adclientId,String createBy,String updatedBy) {
		boolean result = false;
		String uuid = StringUtil.getUUID();
		String lastRest = "Y";
		String sql =  "INSERT INTO Eam_Facilities_MT_Logs (ISACTIVE,Eam_Facilities_MT_Logs_ID,Eam_Facilities_ID, Eam_Maintain_PLAN_ID,AD_USER_ID,Eam_Maintain_ID,TYPE,STATUS,DESCRIPTION,AD_ORG_ID,AD_CLIENT_ID,CREATED,CREATEDBY,UPDATED,UPDATEDBY) " +
				"VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		result = dao.executeSql(sql, lastRest,uuid,facilitiesid,emesmaintainplanid,aduerId,emesmaintainid,Types,status,RemarksNames,adorgId,adclientId,time,createBy,time,updatedBy) > 0;
		
		return result;
	}
	
	//保养计划调用存储过程进行状态改变
	@SuppressWarnings("unchecked")
	public boolean getEquipmentputList(String id,String planid) {
		boolean result = false;
		
		result = dao.executeSql("{call Emes_P_UPDATE_EmesMPD(?,?)}",id,planid) > 0;
		
		return result;
	}
	
	//维修计划调用存储过程进行状态改变
	@SuppressWarnings("unchecked")
	public boolean getEquipmentputLists(String id) {
		boolean result = false;
		
		result = dao.executeSql("{call Emes_P_UPDATE_REPAIR(?)}",id) > 0;
		
		return result;
	}
	
	//查询CREATEBY和UPDATEBY的ID值
	public List<Map<String,Object>> getEquipmentputCreateby(String userIdtime){
		return dao.query("select CREATEDBY as CREATEDBY, UPDATEDBY as UPDATEDBY from ad_user where ad_user_id = ?",userIdtime);
	}

}
